<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: frontpage.php 1203 2008-07-10 08:20:27Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 */

function showJoomleagueStart( &$fp_config )
{
	global $database, $mainframe;
	// we need this for output functions:
	global $joomleague, $overall_config, $htmlHelper;
  
	//add the css
  HTML_joomleague::loadJLCss();
  
	if ($fp_config['fp_show_quick_jump']==1) {
		jl_quick_jump();
	}
	
	// get seasons
	$query = " SELECT s.id, s.name 
	           FROM #__joomleague_seasons AS s
             LEFT JOIN #__joomleague AS p ON p.season_id = s.id
             WHERE p.published = 1 
             GROUP BY s.id 
             ORDER BY s.ordering ";	
	if ( $fp_config['fp_max_seasons_show'] != "" ) {
	 $query .= " LIMIT " . intval( $fp_config['fp_max_seasons_show'] );
	}
	$database->setQuery($query);
	
	if ( $season_list = $database->loadObjectList() ) 
	{		
		$joomleague_tabs = new JLTabs( 0 );
        $joomleague_tabs->startPane( 'allseasons' ); 
        
		foreach ($season_list AS $season) 
		{
			// get season projects
			$allprojects = jl_get_frontpage_projects( $season->id, intval($fp_config['fp_max_project_per_season']));
			if ( count( $allprojects ) > 0 ) 
			{
				$joomleague_tabs->startTab( $season->name, 'season'.$season->id );

				for ( $x=0; $x < count( $allprojects ); $x++ )
				{
					$status = ( $x < intval( $fp_config['fp_count_projects_visible'] ) ) ? 'block' : 'none';
					$p = $allprojects[$x];
					$joomleague = new JoomLeague( $database );
					$joomleague->load( $p->id );
          
					$overall_config = $joomleague->getTemplateConfig("overall.tpl");					 
					$fpage_config = $joomleague->getTemplateConfig("show_frontpage.tpl");
					 					
                    $round = $joomleague->current_round;
                    $current_round=$round;
            
					if ( $fpage_config['show_ranking_table'] == 1 
					   && $joomleague->project_type != 'SIMPLE_KO' ) 
					{
						$table_config = $joomleague->getTemplateConfig("show_table.tpl");
						$table_config = array_merge( $table_config, $fpage_config );
						$table_config['column_sorting'] = 0;
												
						$teams = $joomleague->getTeams();
						$rounds = $joomleague->getRoundNames();
						$current_ranking = array();
						$type = 0;
						
						if ( $joomleague->project_type=='DIVISIONS_LEAGUE' ) 
						{
							$divLevel = intval( $table_config['default_division_view'] );
							if ( $divLevel ) $divisions = $joomleague->getDivisionsId( $divLevel );
							else $divisions = array(0); //project
						}
						else $divisions = array(0); //project
						
						$last = intval( mosGetParam( $_REQUEST, 'last', 0 ) );
						$ranking = new Ranking();
						$ranking->setProjectConfig( $joomleague, $table_config['rankingorder'] );
						
						$previous_ranking=array();
						foreach ( $divisions as $division )
						{
							$current_ranking[$division] = $ranking->getRanking( 0, $round, 1, 
							                     $table_config['show_negative_points'], 
							                     $table_config['sort_table'], 
							                     $division );
							                     
							if ( $table_config['last_ranking'] == 1 )
							{
								if ( $round == 1 ) {//no previous ranking...
									$previous_ranking[$division] =& $current_ranking[$division];
								}
								else {
								  $previous_ranking[$division] = $ranking->getRanking( 0, 
								                                                       $round-1, 
								                                                       1, 
								                                                       $table_config['show_negative_points'], 
								                                                       $table_config['sort_table'], 
								                                                       $division );
								}
							}
						}
					}
					
					if ( $fpage_config['show_results_table'] == 1 ) 
					{
						$results_config = $joomleague->getTemplateConfig("show_results.tpl");
						$results_config = array_merge( $results_config, $fpage_config );
						$results_config['names' ]= $fpage_config['res_names'];
						$results_config['show_icons'] = $fpage_config['res_show_icons'];

						$rounds = $joomleague->getRoundNames();
						//	    		  $matches = $joomleague->getResults($round);
						//            $next_matches = $joomleague->getResults($round + 1);

						//get matches
						$matches = $joomleague->getResultsRows( $joomleague->current_round, 0, $results_config );
						$next_matches = $joomleague->getResultsRows( $joomleague->current_round + 1, 0, $results_config );
						//get teams
						$resultTeams = $joomleague->getTeamsIndexedById(null, 0);

						$colors = $joomleague->getColors($table_config['colors']);
						$query = "SELECT info FROM #__joomleague_team_joomleague WHERE project_id = ".$joomleague->id;
						$database->setQuery($query);
						$result = $database->loadObjectList();
					}
					include (JL_FRONTEND_PATH.'/frontend/default'.'/show_frontpage.tpl.php');
				}
                $joomleague_tabs->endTab();
            }
		}
		$joomleague_tabs->endPane();
		unset( $joomleague );
		unset( $matches );
		unset( $next_matches );
		unset( $rounds );
		unset( $ranking );
	}


	else
	{
		$mainframe->setPageTitle(_JL_PAGE_TITLE_ABOUT);
		$v = new JoomLeagueVersion();
		require(getTemplateFile(JL_FRONTEND_PATH.'/frontend/default', 'no_projects.tpl.php'));
	}
}
?>
